package com.zhaojonhy.zhaopangwb.ui;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.exception.WeiboException;
import com.sina.weibo.sdk.net.RequestListener;
import com.sina.weibo.sdk.openapi.LogoutAPI;
import com.zhaojonhy.zhaopangwb.Fragment.FragmentController;
import com.zhaojonhy.zhaopangwb.Fragment.HomeFragment;
import com.zhaojonhy.zhaopangwb.utils.AccessTokenKeeper;
import com.zhaojonhy.zhaopangwb.utils.Constants;
import com.zhaojonhy.zhaopangwb.utils.TitleBuilder;
import com.zhaojonhy.zhaopangwb.utils.ToastUtils;
import com.zhaojonhy.zhaopangwb.widget.CallBackInterface;
import com.zhaojonhy.zhaopangwb.widget.FragmentBack;
import com.zhaojonhy.zhaopangwb.R;

public class MainActivity extends FragmentActivity implements View.OnClickListener,
        RadioGroup.OnCheckedChangeListener,CallBackInterface {
    private static final String TAG = MainActivity.class.getSimpleName() ;

    private static final int REQUEST_CODE_WRITE_STATUS = 110 ;

    private RadioGroup rgTap ;
    private ImageView ivAdd ;
    private FragmentController controller ;
    private static SlidingMenu menu ;
    private Button button ;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_main);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//屏幕竖向显示
        controller = new FragmentController(this,R.id.flContent) ;
//        controller = FragmentController.getInstance(this,R.id.flContent) ;
        controller.showFragment(0);
        initView() ;
        setSlidingMenu() ;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivAdd :
                Intent intent = new Intent(MainActivity.this, WriteStatusActivity.class);
                startActivityForResult(intent, REQUEST_CODE_WRITE_STATUS);
                break;
            case R.id.user_logout :
                Log.d(TAG,"user_logout:"+"YES") ;
                ToastUtils.showToast(this,"退出", Toast.LENGTH_LONG);
                logOut() ;
                break;
            default:
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.rdHome :
                controller.showFragment(0);
                break;
            case R.id.rdMessage :
                controller.showFragment(1);
                break;
            case R.id.rdSearch :
                controller.showFragment(2);
                break;
            case R.id.rdSelfinfo :
                controller.showFragment(3);
                break;
            default:
                break;
        }
    }
    private void initView(){
        rgTap = (RadioGroup)findViewById(R.id.mainMenu) ;
        ivAdd = (ImageView)findViewById(R.id.ivAdd) ;

        rgTap.setOnCheckedChangeListener(this);
        ivAdd.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (this.isFinishing()){
            return;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // 如果Back键返回,取消发评论等情况,则直接return,不做后续处理
        if(resultCode == RESULT_CANCELED) {
            return;
        }

        switch (requestCode) { //根据返回的数据判断

            case REQUEST_CODE_WRITE_STATUS :

                //如果评论发送成功的返回结果，则重新加载最新评论，同时要求滚动至评论部分
                boolean sendSatusSuccess = data.getBooleanExtra("sendSatusSuccess" ,false) ;
                if(sendSatusSuccess){
                    FragmentManager fragmentManager = getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    HomeFragment homeFragment = (HomeFragment)controller.getFragment(0) ;
                    fragmentTransaction.show(homeFragment) ;
                    fragmentTransaction.commit() ;
                    homeFragment.onBack();
                    RadioButton homeButoon = (RadioButton)findViewById(R.id.rdHome) ;
                    homeButoon.setChecked(true);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void callback() {
        menu.toggle(true);
    }

    private void setSlidingMenu(){
        //设置侧滑菜单
        menu = new SlidingMenu(this);
        menu.setMode(SlidingMenu.LEFT);
        // 设置触摸屏幕的模式
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        // 根据资源文件 ID 设置阴影部分的 width
        menu.setShadowWidthRes(R.dimen.shadow_width);
        // 根据资源文件 ID 设置阴影部分的效果
        menu.setShadowDrawable(R.color.bg_gray);

        // 设置滑动菜单视图的宽度
        menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        // 设置渐入渐出效果的值
        menu.setFadeDegree(0.35f);
        /**
         * SLIDING_WINDOW will include the Title/ActionBar in the content
         * section of the SlidingMenu, while SLIDING_CONTENT does not.
         */
        menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        //为侧滑菜单设置布局
        menu.setMenu(R.layout.left_menu);
        button = (Button)menu.findViewById(R.id.user_logout) ;
        button.setOnClickListener(this);
    }

    private void logOut(){
        Oauth2AccessToken mAccessToken = AccessTokenKeeper.readAccessToken(this) ;
        LogoutAPI logoutAPI = new LogoutAPI(this, Constants.APP_KEY, mAccessToken) ;
        logoutAPI.logout(new RequestListener() {
            @Override
            public void onComplete(String s) {
                Intent intent = new Intent(MainActivity.this,OauthActivity.class) ;
                startActivity(intent);
                finish() ;
                Log.d(TAG,"user_logout:"+"successful") ;
            }

            @Override
            public void onWeiboException(WeiboException e) {
                Log.d(TAG,"user_logout:"+e.getMessage()) ;
            }
        });
    }
}
